package org.apache.harmony.javax.security.auth.login;

import java.io.IOException;
import java.security.AccessControlContext;
import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.security.Security;
import java.util.HashMap;
import java.util.Map;
import org.apache.harmony.javax.security.auth.AuthPermission;
import org.apache.harmony.javax.security.auth.Subject;
import org.apache.harmony.javax.security.auth.callback.Callback;
import org.apache.harmony.javax.security.auth.callback.CallbackHandler;
import org.apache.harmony.javax.security.auth.callback.UnsupportedCallbackException;
import org.apache.harmony.javax.security.auth.spi.LoginModule;

public class LoginContext
{
  private static final String DEFAULT_CALLBACK_HANDLER_PROPERTY = "auth.login.defaultCallbackHandler";
  private static final int OPTIONAL = 0;
  private static final int REQUIRED = 1;
  private static final int REQUISITE = 2;
  private static final int SUFFICIENT = 3;
  private CallbackHandler callbackHandler;
  private ClassLoader contextClassLoader;
  private boolean loggedIn;
  private Module[] modules;
  private Map<String, ?> sharedState;
  private Subject subject;
  private AccessControlContext userContext;
  private boolean userProvidedConfig;
  private boolean userProvidedSubject;

  public LoginContext(String paramString)
    throws LoginException
  {
    init(paramString, null, null, null);
  }

  public LoginContext(String paramString, Subject paramSubject)
    throws LoginException
  {
    if (paramSubject == null)
      throw new LoginException("auth.03");
    init(paramString, paramSubject, null, null);
  }

  public LoginContext(String paramString, Subject paramSubject, CallbackHandler paramCallbackHandler)
    throws LoginException
  {
    if (paramSubject == null)
      throw new LoginException("auth.03");
    if (paramCallbackHandler == null)
      throw new LoginException("auth.34");
    init(paramString, paramSubject, paramCallbackHandler, null);
  }

  public LoginContext(String paramString, Subject paramSubject, CallbackHandler paramCallbackHandler, Configuration paramConfiguration)
    throws LoginException
  {
    init(paramString, paramSubject, paramCallbackHandler, paramConfiguration);
  }

  public LoginContext(String paramString, CallbackHandler paramCallbackHandler)
    throws LoginException
  {
    if (paramCallbackHandler == null)
      throw new LoginException("auth.34");
    init(paramString, null, paramCallbackHandler, null);
  }

  private void init(String paramString, Subject paramSubject, CallbackHandler paramCallbackHandler, Configuration paramConfiguration)
    throws LoginException
  {
    int i;
    AppConfigurationEntry[] arrayOfAppConfigurationEntry;
    this.subject = paramSubject;
    if (paramSubject != null)
      i = 1;
    while (true)
    {
      this.userProvidedSubject = i;
      if (paramString != null)
        break;
      throw new LoginException("auth.00");
      i = 0;
    }
    if (paramConfiguration == null)
      paramConfiguration = Configuration.getAccessibleConfiguration();
    while (true)
    {
      SecurityManager localSecurityManager = System.getSecurityManager();
      if ((localSecurityManager != null) && (!(this.userProvidedConfig)))
        localSecurityManager.checkPermission(new AuthPermission("createLoginContext." + paramString));
      arrayOfAppConfigurationEntry = paramConfiguration.getAppConfigurationEntry(paramString);
      if (arrayOfAppConfigurationEntry != null)
        break;
      if ((localSecurityManager != null) && (!(this.userProvidedConfig)))
        localSecurityManager.checkPermission(new AuthPermission("createLoginContext.other"));
      arrayOfAppConfigurationEntry = paramConfiguration.getAppConfigurationEntry("other");
      if (arrayOfAppConfigurationEntry != null)
        break;
      throw new LoginException("auth.35 " + paramString);
      this.userProvidedConfig = true;
    }
    this.modules = new Module[arrayOfAppConfigurationEntry.length];
    int j = 0;
    if (j >= this.modules.length);
    while (true)
    {
      do
        try
        {
          AccessController.doPrivileged(new PrivilegedExceptionAction(this, paramCallbackHandler)
          {
            public Void run()
              throws Exception
            {
              LoginContext.access$2(this.this$0, Thread.currentThread().getContextClassLoader());
              if (LoginContext.access$1(this.this$0) == null)
                LoginContext.access$2(this.this$0, ClassLoader.getSystemClassLoader());
              if (this.val$cbHandler == null)
              {
                String str = Security.getProperty("auth.login.defaultCallbackHandler");
                if ((str == null) || (str.length() == 0))
                  label57: return null;
                Class localClass = Class.forName(str, true, LoginContext.access$1(this.this$0));
                LoginContext.access$3(this.this$0, (CallbackHandler)localClass.newInstance());
              }
              while (true)
              {
                break label57:
                LoginContext.access$3(this.this$0, this.val$cbHandler);
              }
            }
          });
          if (this.userProvidedConfig)
          {
            this.userContext = AccessController.getContext();
            return;
            this.modules[j] = new Module(this, arrayOfAppConfigurationEntry[j]);
            ++j;
          }
        }
        catch (PrivilegedActionException localPrivilegedActionException)
        {
          Throwable localThrowable = localPrivilegedActionException.getCause();
          throw ((LoginException)new LoginException("auth.36").initCause(localThrowable));
        }
      while (this.callbackHandler == null);
      this.userContext = AccessController.getContext();
      this.callbackHandler = new ContextedCallbackHandler(this, this.callbackHandler);
    }
  }

  private void loginImpl()
    throws LoginException
  {
    int l;
    int[] arrayOfInt3;
    int i1;
    Module[] arrayOfModule2;
    int i3;
    Module[] arrayOfModule3;
    int i5;
    if (this.subject == null)
      this.subject = new Subject();
    if (this.sharedState == null)
      this.sharedState = new HashMap();
    Object localObject = null;
    int[] arrayOfInt1 = new int[4];
    int[] arrayOfInt2 = new int[4];
    Module[] arrayOfModule1 = this.modules;
    int i = arrayOfModule1.length;
    int j = 0;
    if (j >= i)
    {
      l = 1;
      if ((arrayOfInt1[1] == arrayOfInt2[1]) && (arrayOfInt1[2] == arrayOfInt2[2]))
      {
        if ((arrayOfInt2[1] != 0) || (arrayOfInt2[2] != 0))
          break label418;
        if ((arrayOfInt1[0] != 0) || (arrayOfInt1[3] != 0))
          l = 0;
      }
      arrayOfInt3 = new int[4];
      arrayOfInt2[3] = 0;
      arrayOfInt2[2] = 0;
      arrayOfInt2[1] = 0;
      arrayOfInt2[0] = 0;
      if (l == 0)
      {
        arrayOfModule3 = this.modules;
        int i4 = arrayOfModule3.length;
        i5 = 0;
        if (i5 < i4)
          break label424;
      }
      i1 = 1;
      if ((arrayOfInt3[1] == arrayOfInt2[1]) && (arrayOfInt3[2] == arrayOfInt2[2]))
      {
        if ((arrayOfInt2[1] != 0) || (arrayOfInt2[2] != 0))
          break label504;
        if ((arrayOfInt3[0] != 0) || (arrayOfInt3[3] != 0))
          i1 = 0;
      }
      if (i1 == 0)
        break label563;
      arrayOfModule2 = this.modules;
      int i2 = arrayOfModule2.length;
      i3 = 0;
      if (i3 < i2)
        break label510;
      if ((localObject instanceof PrivilegedActionException) && (((Throwable)localObject).getCause() != null))
        localObject = ((Throwable)localObject).getCause();
      if (!(localObject instanceof LoginException))
        break label546;
      throw ((LoginException)localObject);
    }
    Module localModule1 = arrayOfModule1[j];
    try
    {
      int i10;
      do
      {
        localModule1.create(this.subject, this.callbackHandler, this.sharedState);
        if (!(localModule1.module.login()))
          break;
        int i8 = localModule1.getFlag();
        arrayOfInt2[i8] = (1 + arrayOfInt2[i8]);
        int i9 = localModule1.getFlag();
        arrayOfInt1[i9] = (1 + arrayOfInt1[i9]);
        i10 = localModule1.getFlag();
      }
      while (i10 == 3);
      label546: label418: label424: label504: label510: ++j;
    }
    catch (Throwable localThrowable1)
    {
      while (true)
      {
        while (true)
          do
          {
            while (true)
            {
              if (localObject == null)
                localObject = localThrowable1;
              if (localModule1.klass != null)
                break;
              arrayOfInt2[1] = (1 + arrayOfInt2[1]);
            }
            int k = localModule1.getFlag();
            arrayOfInt2[k] = (1 + arrayOfInt2[k]);
          }
          while (localModule1.getFlag() != 2);
        l = 0;
      }
      Module localModule3 = arrayOfModule3[i5];
      if (localModule3.klass != null)
      {
        int i6 = localModule3.getFlag();
        arrayOfInt2[i6] = (1 + arrayOfInt2[i6]);
      }
      try
      {
        localModule3.module.commit();
        int i7 = localModule3.getFlag();
        arrayOfInt3[i7] = (1 + arrayOfInt3[i7]);
        ++i5;
      }
      catch (Throwable localThrowable3)
      {
        while (true)
        {
          while (true)
          {
            if (localObject != null);
            localObject = localThrowable3;
          }
          i1 = 0;
        }
        Module localModule2 = arrayOfModule2[i3];
        try
        {
          localModule2.module.abort();
          ++i3;
        }
        catch (Throwable localThrowable2)
        {
          while (true)
          {
            if (localObject != null);
            localObject = localThrowable2;
          }
          throw ((LoginException)new LoginException("auth.37").initCause((Throwable)localObject));
        }
      }
    }
    label563: this.loggedIn = true;
  }

  private void logoutImpl()
    throws LoginException
  {
    int i = 0;
    if (this.subject == null)
      throw new LoginException("auth.38");
    this.loggedIn = false;
    Object localObject = null;
    int j = 0;
    Module[] arrayOfModule = this.modules;
    int k = arrayOfModule.length;
    if (i >= k)
    {
      if ((localObject == null) && (j != 0))
        return;
      if ((localObject instanceof PrivilegedActionException) && (((Throwable)localObject).getCause() != null))
        localObject = ((Throwable)localObject).getCause();
      if (!(localObject instanceof LoginException))
        break label122;
      throw ((LoginException)localObject);
    }
    Module localModule = arrayOfModule[i];
    try
    {
      localModule.module.logout();
      ++j;
      label122: ++i;
    }
    catch (Throwable localThrowable)
    {
      while (true)
      {
        if (localObject != null);
        localObject = localThrowable;
      }
      throw ((LoginException)new LoginException("auth.37").initCause((Throwable)localObject));
    }
  }

  public Subject getSubject()
  {
    Subject localSubject;
    if ((this.userProvidedSubject) || (this.loggedIn))
      localSubject = this.subject;
    while (true)
    {
      return localSubject;
      localSubject = null;
    }
  }

  public void login()
    throws LoginException
  {
    2 local2 = new PrivilegedExceptionAction(this)
    {
      public Void run()
        throws LoginException
      {
        LoginContext.access$4(this.this$0);
        return null;
      }
    };
    try
    {
      if (this.userProvidedConfig)
        AccessController.doPrivileged(local2, this.userContext);
      else
        AccessController.doPrivileged(local2);
    }
    catch (PrivilegedActionException localPrivilegedActionException)
    {
      throw ((LoginException)localPrivilegedActionException.getException());
    }
  }

  public void logout()
    throws LoginException
  {
    3 local3 = new PrivilegedExceptionAction(this)
    {
      public Void run()
        throws LoginException
      {
        LoginContext.access$5(this.this$0);
        return null;
      }
    };
    try
    {
      if (this.userProvidedConfig)
        AccessController.doPrivileged(local3, this.userContext);
      else
        AccessController.doPrivileged(local3);
    }
    catch (PrivilegedActionException localPrivilegedActionException)
    {
      throw ((LoginException)localPrivilegedActionException.getException());
    }
  }

  private class ContextedCallbackHandler
  implements CallbackHandler
  {
    private final CallbackHandler hiddenHandlerRef;

    ContextedCallbackHandler(, CallbackHandler paramCallbackHandler)
    {
      this.hiddenHandlerRef = paramCallbackHandler;
    }

    public void handle()
      throws IOException, UnsupportedCallbackException
    {
      try
      {
        AccessController.doPrivileged(new PrivilegedExceptionAction(this, paramArrayOfCallback)
        {
          public Void run()
            throws IOException, UnsupportedCallbackException
          {
            LoginContext.ContextedCallbackHandler.access$0(this.this$1).handle(this.val$callbacks);
            return null;
          }
        }
        , LoginContext.access$0(this.this$0));
        return;
      }
      catch (PrivilegedActionException localPrivilegedActionException)
      {
        if (localPrivilegedActionException.getCause() instanceof UnsupportedCallbackException)
          throw ((UnsupportedCallbackException)localPrivilegedActionException.getCause());
        throw ((IOException)localPrivilegedActionException.getCause());
      }
    }
  }

  private final class Module
  {
    AppConfigurationEntry entry;
    int flag;
    Class<?> klass;
    LoginModule module;

    Module(, AppConfigurationEntry paramAppConfigurationEntry)
    {
      this.entry = paramAppConfigurationEntry;
      AppConfigurationEntry.LoginModuleControlFlag localLoginModuleControlFlag = paramAppConfigurationEntry.getControlFlag();
      if (localLoginModuleControlFlag == AppConfigurationEntry.LoginModuleControlFlag.OPTIONAL)
        this.flag = 0;
      while (true)
      {
        while (true)
        {
          while (true)
          {
            return;
            if (localLoginModuleControlFlag != AppConfigurationEntry.LoginModuleControlFlag.REQUISITE)
              break;
            this.flag = 2;
          }
          if (localLoginModuleControlFlag != AppConfigurationEntry.LoginModuleControlFlag.SUFFICIENT)
            break;
          this.flag = 3;
        }
        this.flag = 1;
      }
    }

    // ERROR //
    void create(, CallbackHandler paramCallbackHandler, Map<String, ?> paramMap)
      throws LoginException
    {
      // Byte code:
      //   0: aload_0
      //   1: getfield 27	org/apache/harmony/javax/security/auth/login/LoginContext$Module:entry	Lorg/apache/harmony/javax/security/auth/login/AppConfigurationEntry;
      //   4: invokevirtual 61	org/apache/harmony/javax/security/auth/login/AppConfigurationEntry:getLoginModuleName	()Ljava/lang/String;
      //   7: astore 4
      //   9: aload_0
      //   10: getfield 63	org/apache/harmony/javax/security/auth/login/LoginContext$Module:klass	Ljava/lang/Class;
      //   13: ifnonnull +20 -> 33
      //   16: aload_0
      //   17: aload 4
      //   19: iconst_0
      //   20: aload_0
      //   21: getfield 22	org/apache/harmony/javax/security/auth/login/LoginContext$Module:this$0	Lorg/apache/harmony/javax/security/auth/login/LoginContext;
      //   24: invokestatic 67	org/apache/harmony/javax/security/auth/login/LoginContext:access$1	(Lorg/apache/harmony/javax/security/auth/login/LoginContext;)Ljava/lang/ClassLoader;
      //   27: invokestatic 73	java/lang/Class:forName	(Ljava/lang/String;ZLjava/lang/ClassLoader;)Ljava/lang/Class;
      //   30: putfield 63	org/apache/harmony/javax/security/auth/login/LoginContext$Module:klass	Ljava/lang/Class;
      //   33: aload_0
      //   34: getfield 75	org/apache/harmony/javax/security/auth/login/LoginContext$Module:module	Lorg/apache/harmony/javax/security/auth/spi/LoginModule;
      //   37: ifnonnull +36 -> 73
      //   40: aload_0
      //   41: aload_0
      //   42: getfield 63	org/apache/harmony/javax/security/auth/login/LoginContext$Module:klass	Ljava/lang/Class;
      //   45: invokevirtual 79	java/lang/Class:newInstance	()Ljava/lang/Object;
      //   48: checkcast 81	org/apache/harmony/javax/security/auth/spi/LoginModule
      //   51: putfield 75	org/apache/harmony/javax/security/auth/login/LoginContext$Module:module	Lorg/apache/harmony/javax/security/auth/spi/LoginModule;
      //   54: aload_0
      //   55: getfield 75	org/apache/harmony/javax/security/auth/login/LoginContext$Module:module	Lorg/apache/harmony/javax/security/auth/spi/LoginModule;
      //   58: aload_1
      //   59: aload_2
      //   60: aload_3
      //   61: aload_0
      //   62: getfield 27	org/apache/harmony/javax/security/auth/login/LoginContext$Module:entry	Lorg/apache/harmony/javax/security/auth/login/AppConfigurationEntry;
      //   65: invokevirtual 85	org/apache/harmony/javax/security/auth/login/AppConfigurationEntry:getOptions	()Ljava/util/Map;
      //   68: invokeinterface 89 5 0
      //   73: return
      //   74: astore 7
      //   76: new 51	LoginException
      //   79: dup
      //   80: new 91	java/lang/StringBuilder
      //   83: dup
      //   84: ldc 93
      //   86: invokespecial 96	java/lang/StringBuilder:<init>	(Ljava/lang/String;)V
      //   89: aload 4
      //   91: invokevirtual 100	java/lang/StringBuilder:append	(Ljava/lang/String;)Ljava/lang/StringBuilder;
      //   94: invokevirtual 103	java/lang/StringBuilder:toString	()Ljava/lang/String;
      //   97: invokespecial 104	LoginException:<init>	(Ljava/lang/String;)V
      //   100: aload 7
      //   102: invokevirtual 108	LoginException:initCause	(Ljava/lang/Throwable;)Ljava/lang/Throwable;
      //   105: checkcast 51	LoginException
      //   108: athrow
      //   109: astore 6
      //   111: new 51	LoginException
      //   114: dup
      //   115: new 91	java/lang/StringBuilder
      //   118: dup
      //   119: ldc 110
      //   121: invokespecial 96	java/lang/StringBuilder:<init>	(Ljava/lang/String;)V
      //   124: aload 4
      //   126: invokevirtual 100	java/lang/StringBuilder:append	(Ljava/lang/String;)Ljava/lang/StringBuilder;
      //   129: invokevirtual 103	java/lang/StringBuilder:toString	()Ljava/lang/String;
      //   132: invokespecial 104	LoginException:<init>	(Ljava/lang/String;)V
      //   135: aload 6
      //   137: invokevirtual 108	LoginException:initCause	(Ljava/lang/Throwable;)Ljava/lang/Throwable;
      //   140: checkcast 51	LoginException
      //   143: athrow
      //   144: astore 5
      //   146: new 51	LoginException
      //   149: dup
      //   150: new 91	java/lang/StringBuilder
      //   153: dup
      //   154: ldc 112
      //   156: invokespecial 96	java/lang/StringBuilder:<init>	(Ljava/lang/String;)V
      //   159: aload 4
      //   161: invokevirtual 100	java/lang/StringBuilder:append	(Ljava/lang/String;)Ljava/lang/StringBuilder;
      //   164: invokevirtual 103	java/lang/StringBuilder:toString	()Ljava/lang/String;
      //   167: invokespecial 104	LoginException:<init>	(Ljava/lang/String;)V
      //   170: aload 5
      //   172: invokevirtual 108	LoginException:initCause	(Ljava/lang/Throwable;)Ljava/lang/Throwable;
      //   175: checkcast 51	LoginException
      //   178: athrow
      //
      // Exception table:
      //   from	to	target	type
      //   16	33	74	java/lang/ClassNotFoundException
      //   40	54	109	java/lang/IllegalAccessException
      //   40	54	144	java/lang/InstantiationException
    }

    int getFlag()
    {
      return this.flag;
    }
  }
}